<template>
  <div>
    <el-date-picker
      v-model="selectshijian"
      type="daterange"
      align="left"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions"
      value-format="yyyy-MM-dd"
    >
    </el-date-picker>
  </div>
</template>


<script>
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      selectshijian: [],
    };
  },
  methods: {
    chushihua() {
      //获取当前日期
      var jintian = new Date();
      var nowY = jintian.getFullYear();
      var nowM = jintian.getMonth() + 1;
      var nowD = jintian.getDate();
      var enddate =
        nowY +
        "-" +
        (nowM < 10 ? "0" + nowM : nowM) +
        "-" +
        (nowD < 10 ? "0" + nowD : nowD); //当前日期

      //获取三十天前日期
      var jintianqian30 = new Date(jintian - 1000 * 60 * 60 * 24 * 365 * 3); //365*x表示多少年
      var lastY = jintianqian30.getFullYear();
      var lastM = jintianqian30.getMonth() + 1;
      var lastD = jintianqian30.getDate();
      var startdate =
        lastY +
        "-" +
        (lastM < 10 ? "0" + lastM : lastM) +
        "-" +
        (lastD < 10 ? "0" + lastD : lastD);

      let shijian = [];
      shijian.push(startdate);
      shijian.push(enddate);
      this.selectshijian = shijian;
    },
  },
  watch: {
    selectshijian: {
      handler: function () {
        this.$emit("selectshijianfangfa", this.selectshijian);
      },
    },
  },
  created() {
    this.chushihua();
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
</style>